package nz.co.doherty.gwtpbreadcrumbs.client.ui.home;

import nz.co.doherty.gwtpbreadcrumbs.client.NameTokens;
import nz.co.doherty.gwtpbreadcrumbs.client.gin.ClientGinjector;
import nz.co.doherty.gwtpbreadcrumbs.client.i8n.ClientConstants;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.app.AppPresenter;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.reset.ResetDataAction;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.reset.ResetDataResult;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtp.mvp.client.annotations.TitleFunction;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceRequest;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyPlace;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealContentEvent;

public class HomePresenter
		extends PresenterImpl<HomeView, HomePresenter.HomeProxy>
		implements HomeView.Presenter {

	@ProxyCodeSplit
	@NameToken(NameTokens.home)
	public interface HomeProxy extends ProxyPlace<HomePresenter> {
	}

	private final PlaceManager placeManager;
	private final ClientConstants constants;
	private final DispatchAsync dispatcher;

	@Inject
	public HomePresenter(EventBus eventBus, HomeView view, HomeProxy proxy,
			PlaceManager placeManager, ClientConstants constants,
			DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.placeManager = placeManager;
		this.constants = constants;
		this.dispatcher = dispatcher;
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReveal() {
		super.onReveal();

		view.setListProductsToken(placeManager
				.buildRelativeHistoryToken(new PlaceRequest(
						NameTokens.productList)));
		view.setListCustomersToken(placeManager
				.buildRelativeHistoryToken(new PlaceRequest(
						NameTokens.customerList)));
		view.setListOrdersToken(placeManager
				.buildRelativeHistoryToken(new PlaceRequest(
						NameTokens.orderList)));
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(eventBus, AppPresenter.TYPE_RevealMainContent,
			this);
	}

	@TitleFunction
	public static String getPageTitle(ClientGinjector ginjector) {
		return ginjector.getConstants().home();
	}

	@Override
	public void onResetData() {
		this.dispatcher.execute(new ResetDataAction(),
			new AsyncCallback<ResetDataResult>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(ResetDataResult result) {
					// TODO Auto-generated method stub
				}
			});
	}
}
